home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
inpfiel.exe
/
INPFIELD.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-23
|
5KB
|
177 lines
// inpfield.cpp - generic input line class implementation
//
// Christensen OnLine
//
//$Log: inpfield.cpp $
//Revision 1.4 1993/01/23 14:38:15 matt
//Uploaded to CIS
//
//Revision 1.3 1993/01/23 10:07:18 matt
//Experiments with selectAll
//
//Revision 1.2 1993/01/22 21:28:33 matt
//Name changes, getText routines
//
//Revision 1.1 1993/01/13 20:56:46 matt
//Input field with validation
//
#include <string.h>
#include <stdlib.h>
#include <strstream.h>
#include "inpfield.h"
///////////////////////////////////////////////////////////////////////
// InpField
///////////////////////////////////////////////////////////////////////
InpField::InpField(
int orgX, int orgY, InpData *theInpData, short aDataLen, const char *aLabel
) :
inpData(theInpData), dataLen(aDataLen),
TGroup(TRect(orgX,orgY,orgX,orgY))
{
// Find length of label, not including ~ chrs.
//
short lablen = 0;
for (int i=0; *(aLabel+i); i++)
if (*(aLabel+i) != '~')
lablen++;
// Now that we know how long the label is, grow the TGroup to a size
// large enough to accomodate the TLabel and TInputLine (below).
//
growTo(lablen + dataLen + 4, 1);
// Create TInputLine member, and TLabel if any provided
//
inpLine = new TInputLine(
TRect(lablen+1, 0, lablen+dataLen+4, 1),
dataLen+1 // Must add space for NULL - documentation wrong
);
insert(inpLine);
inpLine->setData(inpData->getText(dataLen));
inpLine->setState(sfSelected,False);
dataSynced = True;
if (lablen) {
TLabel *labptr =
new TLabel(TRect(0, 0, lablen+1, 1), aLabel, inpLine);
insert(labptr);
}
}
void InpField::handleEvent(TEvent& event)
{
if (event.what & evKeyboard) {
// Input data object (inpData) no longer corresponds to
// displayed TInputLine (inpLine). Re-fresh may be needed
// when focus is lost.
//
dataSynced = False;
switch (event.keyDown.charScan.charCode) {
case '\r':
case '\t':
// If valid input, assign internal value (inpData) to InpLine
// input string. Otherwise, display message box with inpData's
// supplied error message.
//
char buf[InpDataMAXLEN+1];
inpLine->getData(buf);
if (inpData->isValid(buf)) {
*inpData = buf;
}
else {
messageBox(inpData->errMsg(), mfOKButton|mfError);
clearEvent(event);
}
break;
}
}
else if (event.what & evMessage) {
switch (event.message.command) {
case cmReceivedFocus:
// Highlight input line
//
if (event.message.infoPtr == inpLine) {
inpLine->selectAll(True);
inpLine->setState(sfSelected,True);
}
break;
case cmReleasedFocus:
if (event.message.infoPtr == inpLine) {
// If inpLine was edited in any way, reset it to the internally
// stored value. If the inpLine entry has not yet been
// validated (\t or \r key event), then the displayed value
// will change back to the internally stored value (inpData).
//
if (!dataSynced) {
dataSynced = True;
inpLine->setData(inpData->getText(dataLen));
}
inpLine->selectAll(False);
inpLine->setState(sfSelected,False);
}
break;
}
}
TGroup::handleEvent(event);
}
TPalette& InpField::getPalette() const
{
static TPalette palette(cpInpField, sizeof(cpInpField));
return palette;
}
///////////////////////////////////////////////////////////////////////
// IntInpData
///////////////////////////////////////////////////////////////////////
char InpData::convBuf[InpDataMAXLEN+1];
IntInpData::IntInpData(int initValue, int aMin, int aMax) :
value(initValue), min(aMin), max(aMax)
{
}
int IntInpData::isValid(char *str)
{
int val = atoi(str);
return (val >= min && val <= max);
}
InpData& IntInpData::operator = (char *str)
{
value = atoi(str);
return *this;
}
char * IntInpData::getText(short len)
{
strstream buf(convBuf, InpDataMAXLEN, ios::out);
buf << value << ends;
int vallen=strlen(convBuf);
if (len <= InpDataMAXLEN && len >= vallen) {
for (int i=vallen; i<len; i++) convBuf[i] = ' ';
convBuf[i] = '\0';
}
else {
messageBox("IntInpData::getText - requested len invalid",
mfOKButton|mfError);
strcpy(convBuf,"*");
}
return convBuf;
}
const char * IntInpData::errMsg()
{
strstream buf(convBuf, InpDataMAXLEN, ios::out);
buf<<"Value must be between "<<min<<" and "<<max<<" inclusive"<<ends;
return convBuf;
}